
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class TestDocParser extends DefaultHandler{

	ArrayList<DocumentEntity> xmlDocs;
	private static HashMap<Integer, DocumentEntity> parsedTestDoc = new HashMap<Integer, DocumentEntity>();
	
	
	private String tempVal, className;
	private StringBuffer tempValBuf = new StringBuffer();
	private String docName;
	private int testSetSize = 0, currentID;
	
	private static HashMap<String, ClassDetails> classInfoMap = new HashMap<String, ClassDetails>();
	
	//to maintain context
	public DocumentEntity tempDoc;
	
	
	
	
	public TestDocParser(){
		xmlDocs = new ArrayList<DocumentEntity>();
	}
	
		

	public HashMap<Integer, DocumentEntity> parseDocument(String path) {
		//get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
		
			//get a new instance of parser
			SAXParser sp = spf.newSAXParser();
			
			//parse the file and also register this class for call backs
			//read the documents of the training set
			File testSetPath = new File(path);

		    File [] files = testSetPath.listFiles();
		    
		    
		    for (int i = 0; i < files.length; i++){
		        if (files[i].isFile()){ 
		            docName = files[i].toString();
		          //parse the file
		            sp.parse(docName, this);
		            
		        }
		    }
			
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch (IOException ie) {
			ie.printStackTrace();
		}
		
		//testSetSize = xmlDocs.size();
		//System.out.println("Test set size:"+testSetSize);
		return parsedTestDoc;
	}

	/**
	 * Iterate through the list and print
	 * the contents
	 */
	public void printData(){
		
		Iterator<DocumentEntity> it = xmlDocs.iterator();
		while(it.hasNext()) {
			System.out.println(it.next().toString());
		}
	}
	

	//Event Handlers
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		//reset
		tempVal = "";
		tempValBuf = new StringBuffer();
		if(qName.equalsIgnoreCase("Reuters")) {
			//create a new instance of article
			tempDoc = new DocumentEntity();
			tempDoc.setID(Integer.parseInt(attributes.getValue("NEWID")));
			
		}
	}
	

	public void characters(char[] ch, int start, int length) throws SAXException {
		tempVal = new String(ch,start,length);
		tempValBuf.append(ch, start, length);
		
		
	}

	
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if(qName.equalsIgnoreCase("Reuters")) {
			//add it to the list
			xmlDocs.add(tempDoc);	
			currentID = tempDoc.getID();
			parsedTestDoc.put(currentID, tempDoc);
			
		}else if (qName.equalsIgnoreCase("body")) {
			tempDoc.setBody(tempValBuf.toString());			
		}
		
		
	}
	

}




